﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Windows
{
    public partial class FormProcedureUpdate : DevExpress.XtraEditors.XtraForm
    {
        private readonly IProcedurePersist _procedurePersist = null;
        private readonly TProcedureModel _oldModel = null;
        public FormProcedureUpdate(TProcedureModel needUpdateModel)
        {
            _oldModel = needUpdateModel;
            _procedurePersist = InstanceFactory.GetProcedurePersist();
            InitializeComponent();
            LoadModel();
        }

        private  void LoadModel()
        {
            this.textEditProcedureNoOld.Text = _oldModel.ProcedureNo;
            this.textEditProcedureNameOld.Text = _oldModel.ProcedureName;
            this.textEditProcedureNoNew.Text = _oldModel.ProcedureNo;
            this.textEditProcedureNameNew.Text = _oldModel.ProcedureName;
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            try
            {
                if(String.IsNullOrEmpty(this.textEditProcedureNoNew.Text.Trim()))
                {
                    XtraMessageBox.Show("工序代码不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                _procedurePersist.UpdateProcedureModel(this._oldModel.ProcedureNo,this.textEditProcedureNoNew.Text.Trim()
                    ,this.textEditProcedureNameNew.Text.Trim());
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }
    }
}